<?php


namespace app\index\controller;

use think\Db;
use think\Exception;
use think\exception\PDOException;

/**
 * 数据库删除相关
 * Class DbDeleteTest
 * @package app\index\controller
 */
class DbDeleteTest
{
    /**
     * delete
     * sql：DELETE FROM `t_user` WHERE  `id` = 10000034
     * @return \think\response\Json
     */
    public function delete_one_test()
    {
        try {
            $res = Db::table('t_user')
                ->delete(10000034);
            return json(['msg' => '删除成功！', 'data' => $res]);
        } catch (Exception $e) {
            return json(['msg' => '删除失败！', 'data' => $e]);
        }
    }

    /**
     * delete
     * sql：DELETE FROM `t_user` WHERE  `id` IN (10000032,10000033)
     * @return \think\response\Json
     */
    public function delete_mul_test()
    {
        try {
            $res = Db::table('t_user')
                ->delete([10000032, 10000033]);
            return json(['msg' => '删除成功！', 'data' => $res]);
        } catch (Exception $e) {
            return json(['msg' => '删除失败！', 'data' => $e]);
        }
    }

    /**
     * delete
     * sql：DELETE FROM `t_user` WHERE  `id` = 10000031
     * @return \think\response\Json
     */
    public function delete_where_test()
    {
        try {
            $res = Db::table('t_user')
                ->where('id', 10000031)
                ->delete();
            return json(['msg' => '删除成功！', 'data' => $res]);
        } catch (Exception $e) {
            return json(['msg' => '删除失败！', 'data' => $e]);
        }
    }

    /**
     * soft delete
     * sql：UPDATE `t_user`  SET `delete_time` = 1612667369  WHERE  `id` = 10000035
     * @return \think\response\Json
     */
    public function delete_soft_test()
    {
        try {
            $res = Db::table('t_user')
                ->where('id', 10000035)
                ->useSoftDelete('delete_time', time())
                ->delete();
            return json(['msg' => '删除成功！', 'data' => $res]);
        } catch (Exception $e) {
            return json(['msg' => '删除失败！', 'data' => $e]);
        }
    }
}